﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplicationLibrary;
using ProjectManager.Masters.ModuleData;
using ApplicationLibrary.CounterManager;

namespace ProjectManager.Masters.Library
{
    public partial class NotifyForm : WinFormsUI.Docking.DockContent
    {
        enum PlayStatus { stop, play, pause };
        private Timer timer = null;
        private DateTime StartSession, EndSession;

        public Counters FatherType { get; set; }
        public int Father { get; set; }
        public string Description { get { return txtDescription.Text; } set { txtDescription.Text = value; } }
        public bool SessionIsStart { get; set; }

        public NotifyForm()
        {
            InitializeComponent();
            SessionIsStart = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            ChangeButtonStatus(PlayStatus.pause);
            tmdTimer.Value = new TimeSpan(0, 0, 0);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            EndSession = DateTime.Now;
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
            }
            ChangeButtonStatus(PlayStatus.stop);
            this.Close();
        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            EndSession = DateTime.Now;
            ChangeButtonStatus(PlayStatus.pause);
            timer.Stop();
            UpdateSession();
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            SessionIsStart = true;
            StartSession = DateTime.Now;
            CreateTimer();
            ChangeButtonStatus(PlayStatus.play);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            UpdateSession();
            base.OnClosing(e);
        }

        private void ChangeButtonStatus(PlayStatus ps)
        {
            switch(ps)
            {
                case PlayStatus.pause:
                    btnStop.Enabled = true;
                    btnPause.Enabled = false;
                    btnPlay.Enabled = true;
                    break;
                case PlayStatus.stop:
                    btnStop.Enabled = false;
                    btnPause.Enabled = false;
                    btnPlay.Enabled = true;
                    break;
                case PlayStatus.play:
                    btnStop.Enabled = true;
                    btnPause.Enabled = true;
                    btnPlay.Enabled = false;
                    break;
            }
        }

        private void UpdateSession()
        {
            if (!SessionIsStart)
                return;
            SessionIsStart = false;

            DRSession duSession = new DRSession(true);
            duSession.Find(0);
            DataRow dr = duSession.AddRecord();
            CounterManager cm = new CounterManager(Counters.Session.Int(), 0, "Session");
            int ID = cm.GetNewIntValue();
            duSession.SetValue<int>(PM_Session.ID, ID);
            duSession.SetValue<Counters>(PM_Session.Type, FatherType);
            duSession.SetValue<int>(PM_Session.Code, Father);
            duSession.SetValue<string>(PM_Session.Description, txtDescription.Text);
            duSession.SetValue<DateTime>(PM_Session.StartDate, StartSession);
            duSession.SetValue<DateTime>(PM_Session.EndDate, EndSession);
            duSession.Update();
            cm.SetValue(ID);
        }

        private void CreateTimer()
        {
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                return;
            TimeSpan diff = DateTime.Now - StartSession;
            tmdTimer.Value = diff;
        }
    }
}
